package com.unity3d.sqliteapp8;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.facebook.stetho.Stetho;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    UserDaoDatabase userDaoDatabase;
    UserDao userDao;

    //图片循环滚动
    //疑问：图片指示点
    Banner banner;
    List<String>stringList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Stetho.initializeWithDefaults(this);

        banner =findViewById(R.id.banner);
        stringList.add("https://pic1.zhimg.com/v2-d58ce10bf4e01f5086c604a9cfed29f3_r.jpg?source=1940ef5c");
        stringList.add("https://www.keaidian.com/uploads/allimg/190424/24110307_19.jpg");
        stringList.add("https://tse1-mm.cn.bing.net/th/id/OIP-C.nRlAFygdctTCHmIWN7GxRwHaEK?pid=ImgDet&rs=1");

        //泛型
        banner.setAdapter(new BannerImageAdapter<String>(stringList) {

            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                //网络图片加载
                Glide.with(holder.imageView)
                        .load(data)//获取图片URL
                        .into(holder.imageView);
            }
        });
        //添加圆形的指示点
        banner.setIndicator(new CircleIndicator(this));
        //指示点的大小
        banner.setIndicatorRadius(80);
        banner.setBannerRound(30);


        //凡是涉及到连接网络的依赖
        //context:在外部类中传this
        userDaoDatabase = Room.databaseBuilder(this, UserDaoDatabase.class, "Mytest")
                .allowMainThreadQueries()//允许在主线程调用
                .build();

        //返回的不是一个接口对象，返回的是一个接口的实现类  是运行期间动态生成的
        userDao=userDaoDatabase.getUserDao();

        //连接数据库
        /*
        * 1.和数据库建立连接
        * 2.操纵数据库类
        * 3.发送数据库*/

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            //属于内部类（匿名内部类）
            public void onClick(View view) {

                //使用第二种方法：添加数据
                User1 user1=new User1();
                user1.userName="test1234";
                user1.passWord="12345";
                user1.id=2;
                user1.age=20;
                userDao.insertUser(user1);//接口的方法是不能够调用


                /*//参数一：参数二：数据库的名字
                //创建好数据库，并与数据库之间建立连接
                DBOpenHelper dbOpenHelper=new DBOpenHelper(MainActivity.this,"test",null,1);
                //获取到执行数据库类
                SQLiteDatabase sqLiteDatabase=dbOpenHelper.getWritableDatabase();
                //方法一：直接执行数据库执行语句
                //sqLiteDatabase.execSQL("");
                //方法二：
                //类似hashmap，以键值对保存数据  key->数据表列名  value->列的值
                ContentValues contentValues=new ContentValues();
                contentValues.put("username","test1234");
                contentValues.put("password","123456");
                contentValues.put("age",19);
                sqLiteDatabase.insert("user",null,contentValues);*/
            }
        });


        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User1 user1=new User1();
                user1.userName="test1234";
                user1.passWord="12345";
                user1.id=2;
                user1.age=20;
                userDao.deleteUser(user1);
            }
        });
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               List<User1> user1List=userDao.findAllUserById(2);
               for(User1 user1:user1List){
                   Log.i("User1","id= "+user1.id+"username: "+user1.userName);
               }
            }

        });
    }
}